﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using EasyMvcBlog.DTO;
using EasyMvcBlog.Data;
using EasyMvcBlog.Infrastructure.Repository;
using NHibernate.Criterion;
using Webdiyer.WebControls.Mvc;

namespace EasyMvcBlog.Domain
{
    public class SoftwareUseDomain
    {
        private RepositoryBase<SoftwareUse> softwareUseRepository = new RepositoryBase<SoftwareUse>();

        /// <summary>
        /// 添加或更新软件使用信息
        /// </summary>
        /// <param name="softwareUseDto"></param>
        public void AddOrUpdateSoftwareUseInfo(SoftwareUseDTO softwareUseDto)
        {
            Mapper.CreateMap<SoftwareUseDTO, SoftwareUse>();
            SoftwareUse softwareUse = Mapper.Map<SoftwareUseDTO, SoftwareUse>(softwareUseDto);

            //判断是否已经存在该记录
            SoftwareUse isExistRecord = softwareUseRepository.GetList(
                o => o.ComputerMac == softwareUse.ComputerMac && o.SoftwareName == softwareUse.SoftwareName).
                FirstOrDefault();
            if (isExistRecord != null)
            {
                isExistRecord.ComputerName = softwareUse.ComputerName;
                isExistRecord.LastUseTime = DateTime.Now;
                isExistRecord.TotalUseCount += 1;
                softwareUseRepository.Update(isExistRecord);
            }
            else
            {
                //是一个新的用户使用记录
                softwareUse.LastUseTime = DateTime.Now;
                softwareUse.TotalUseCount = 1;
                softwareUseRepository.Add(softwareUse);
            }
        }

        /// <summary>
        /// 获得特定软件的使用信息
        /// </summary>
        /// <returns></returns>
        public PagedList<SoftwareUseDTO> GetSoftwareUseList(int pageIndex, int pageSize, string softwareName)
        {
            List<SoftwareUse> softwareUseList = softwareUseRepository.GetList(o => o.SoftwareName == softwareName).OrderByDescending(o => o.LastUseTime).ToList();
            Mapper.CreateMap<SoftwareUse, SoftwareUseDTO>();
            List<SoftwareUseDTO> softwareDTOList = Mapper.Map<List<SoftwareUse>, List<SoftwareUseDTO>>(softwareUseList);
            return new PagedList<SoftwareUseDTO>(softwareDTOList, pageIndex, pageSize);
        }

        /// <summary>
        /// 获得所有软件的使用信息
        /// </summary>
        /// <returns></returns>
        public PagedList<SoftwareUseDTO> GetSoftwareUseList(int pageIndex, int pageSize)
        {
            List<SoftwareUse> softwareUseList = softwareUseRepository.GetList(o => 1 == 1).OrderByDescending(o => o.LastUseTime).ToList();
            Mapper.CreateMap<SoftwareUse, SoftwareUseDTO>();
            List<SoftwareUseDTO> softwareDTOList = Mapper.Map<List<SoftwareUse>, List<SoftwareUseDTO>>(softwareUseList);
            return new PagedList<SoftwareUseDTO>(softwareDTOList, pageIndex, pageSize);
        }

        public List<string> GetDistSoftwareName()
        {
            return softwareUseRepository.GetAll().Select(o => o.SoftwareName).Distinct().ToList();
        }

        public int GetTodayUsage(string softwareName)
        {
            if (softwareName == "all")
            {
               return softwareUseRepository.GetList(o => o.LastUseTime.Date == DateTime.Now.Date).Count;
            }
            return softwareUseRepository.GetList(o => o.SoftwareName == softwareName && o.LastUseTime.Date == DateTime.Now.Date).Count;
        }
    }
}